package com.yunsc.admin.controller.store;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.system.dto.QueryStoreUserDTO;
import com.yunsc.system.dto.StoreUserDTO;
import com.yunsc.system.entity.YunscStoreUserEntity;
import com.yunsc.system.service.YunscStoreUserService;
import com.yunsc.system.vo.StoreUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-04-04 23:19
 */
@Api(value = "商家用户信息接口", tags = "商家信息接口")
@Slf4j
@RestController
@RequestMapping("store/user")
public class StoreUserController {

    @Resource
    private YunscStoreUserService storeUserService;

    @ApiOperation("查询商家管理员列表")
    @GetMapping("/list")
    public Result<?> getStoreUserList(QueryStoreUserDTO userParams) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        PageResult<YunscStoreUserEntity> pageResult = storeUserService.queryStoreUserList(userParams, currentStoreId);
        return Result.resultPageList(pageResult);
    }

    @ApiOperation("编辑商家管理员")
    @PostMapping("/edit")
    public Result<?> editStoreUser(@RequestBody @Validated({ValidationGroups.Update.class}) StoreUserDTO storeUserDTO) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        storeUserService.editStoreUser(storeUserDTO);
        return Result.ok();
    }

    @ApiOperation("删除商家管理员")
    @PostMapping("/delete")
    public Result<?> removeStoreUser(@RequestBody @Validated({ValidationGroups.Delete.class}) StoreUserDTO storeUserDTO) {
        storeUserService.removeStoreUser(storeUserDTO.getUserId());
        return Result.ok();
    }

    @ApiOperation("添加商家管理员")
    @PostMapping("/add")
    public Result<?> addStoreUser(@RequestBody @Validated({ValidationGroups.Insert.class}) StoreUserDTO storeUserDTO) {
        Integer currentStoreId = SecurityUtil.getCurrentStoreId();
        storeUserService.addStoreUser(storeUserDTO, currentStoreId);
        return Result.ok();
    }

    @ApiOperation("获取商家用户信息")
    @GetMapping("/info")
    public Result<?> getUserInfo() {
        Integer storeUserId = SecurityUtil.getCurrentStoreUserId();
        Integer storeId = SecurityUtil.getCurrentStoreId();
        StoreUserInfoVO userInfoDto = storeUserService.getUserInfo(storeUserId, storeId);
        return Result.ok(userInfoDto);
    }

    // /user/renew

    @ApiOperation("更新商家管理员密码")
    @PostMapping("/renew")
    public Result<?> renewStoreUser(@RequestBody StoreUserDTO param) {
        Integer storeUserId = SecurityUtil.getCurrentStoreUserId();
        storeUserService.renewStoreUser(param, storeUserId);
        return Result.ok();
    }


}
